package com.hele.sts.pub;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.StringTokenizer;
import java.util.Vector;

public class StringTool
{
  public static String GBCode(String Unicode)
  {
    String GBcode = null;
    try
    {
      GBcode = new String(Unicode.getBytes("ISO-8859-1"));
    }
    catch (UnsupportedEncodingException e)
    {
      e.printStackTrace();
    }
    return GBcode;
  }
  
  public static String array2String(String[] saFields)
  {
    StringBuffer sFields = new StringBuffer();
    for (int i = 0; i < saFields.length; i++)
    {
      if (i != 0) {
        sFields.append(",");
      }
      sFields.append(saFields[i]);
    }
    return sFields.toString();
  }
  
  public static final String filterSQ(String in)
  {
    if ((in == null) || (in.length() == 0)) {
      return in;
    }
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < in.length(); i++)
    {
      char cin = in.charAt(i);
      if (cin == '\'') {
        sb.append('\'');
      }
      sb.append(cin);
    }
    return sb.toString();
  }
  
  public static final Vector getTokenString(String inString, String sFlag)
  {
    Vector arr = new Vector();
    String temp = "";
    for (StringTokenizer st = new StringTokenizer(inString, sFlag); st.hasMoreElements(); arr.add(temp)) {
      temp = (String)st.nextElement();
    }
    return arr;
  }
  
  public static boolean isNumber(String sNumber, boolean prefixZeroAllowed)
    throws Exception
  {
    if ((sNumber == null) || (sNumber.equals(""))) {
      return false;
    }
    int len = sNumber.length();
    for (int i = 0; i < len; i++)
    {
      char c = sNumber.charAt(i);
      if ((i == 0) && (!prefixZeroAllowed) && (len > 1) && (c == '0')) {
        return false;
      }
      if (!Character.isDigit(c)) {
        return false;
      }
    }
    return true;
  }
  
  public static boolean isSpace(String s)
    throws Exception
  {
    if ((s == null) || (s.equals(""))) {
      return true;
    }
    int len = s.length();
    for (int i = 0; i < len; i++)
    {
      char c = s.charAt(i);
      if (!Character.isWhitespace(c)) {
        return false;
      }
    }
    return true;
  }
  
//  public static final String replace(String line, String oldString, String newString)
//  {
//    int i = 0;
//    if ((i = line.indexOf(oldString, i)) >= 0)
//    {
//      char[] line2 = line.toCharArray();
//      char[] newString2 = newString.toCharArray();
//      int oLength = oldString.length();
//      StringBuffer buf = new StringBuffer(line2.length);
//      buf.append(line2, 0, i).append(newString2);
//      i += oLength;
//      for (int j = i; (i = line.indexOf(oldString, i)) > 0; j = i)
//      {
//        buf.append(line2, j, i - j).append(newString2);
//        i += oLength;
//      }
//      buf.append(line2, j, line2.length - j);
//      return buf.toString();
//    }
//    return line;
//  }
  
  public static String toBlank(Integer input)
  {
    String BlankVal = null;
    try
    {
      if (input == null) {
        BlankVal = "";
      } else {
        BlankVal = input.toString();
      }
    }
    catch (Exception localException) {}
    return BlankVal;
  }
  
  public static String toBlank(Double input)
  {
    String BlankVal = null;
    try
    {
      if (input == null) {
        BlankVal = "";
      } else {
        BlankVal = input.toString();
      }
    }
    catch (Exception localException) {}
    return BlankVal;
  }
  
  public static String toBlank(Date input)
  {
    String BlankVal = null;
    try
    {
      if (input == null) {
        BlankVal = "";
      } else {
        BlankVal = input.toString();
      }
    }
    catch (Exception localException) {}
    return BlankVal;
  }
  
  public static String toBlank(Float input)
  {
    String BlankVal = null;
    try
    {
      if (input == null) {
        BlankVal = "";
      } else {
        BlankVal = input.toString();
      }
    }
    catch (Exception localException) {}
    return BlankVal;
  }
  
  public static String toBlank(String input)
  {
    String BlankVal = null;
    try
    {
      if (input == null) {
        BlankVal = "";
      } else {
        BlankVal = input;
      }
    }
    catch (Exception localException) {}
    return BlankVal;
  }
  
  public static String toSpace(Double input)
  {
    String SpaceVal = null;
    try
    {
      if ((input == null) || (input.equals(""))) {
        SpaceVal = "&nbsp;";
      } else {
        SpaceVal = input.toString();
      }
    }
    catch (Exception localException) {}
    return SpaceVal;
  }
  
  public static String toSpace(Date input)
  {
    String SpaceVal = null;
    try
    {
      if ((input == null) || (input.equals(""))) {
        SpaceVal = "&nbsp;";
      } else {
        SpaceVal = input.toString();
      }
    }
    catch (Exception localException) {}
    return SpaceVal;
  }
  
  public static String toSpace(Float input)
  {
    String SpaceVal = null;
    try
    {
      if ((input == null) || (input.equals(""))) {
        SpaceVal = "&nbsp;";
      } else {
        SpaceVal = input.toString();
      }
    }
    catch (Exception localException) {}
    return SpaceVal;
  }
  
  public static String toSpace(String input)
  {
    String SpaceVal = null;
    try
    {
      if ((input == null) || (input.trim().equals(""))) {
        SpaceVal = "&nbsp;";
      } else {
        SpaceVal = input;
      }
    }
    catch (Exception localException) {}
    return SpaceVal;
  }
}
